package tools;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitor;
import java.nio.file.Path;

import org.jsoup.HttpStatusException;

import scrapper.AbstractScrapper;
import scrapper.FAScrapper;
import scrapper.PixivScrapper;
import utils.StringUtils;

import files.ProcessFile;

public class ArtistSorter {

	public static void main(String[] args) {
		//String root = "K:\\(｡･｀ω´･｡)\\2d\\kemono";
		String root = "K:\\(｡･｀ω´･｡)\\2d\\human";
		FileVisitor<Path> fileProcessor = null;
		try {
			fileProcessor = new ProcessFile(root, false);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		StringUtils utils = new StringUtils();
		utils.addPattern("^(\\d{6,9})(\\.jpg|\\.png)$", "pixiv");
		utils.addPattern("^(\\d{6,9})_(.*)(\\.jpg|\\.png)$", "pixiv");
		utils.addPattern("^(\\d{6,9})\\s(.*)(\\.jpg|\\.png)$", "pixiv");
		utils.addPattern("^(\\d{10})\\.(.*)_(.*)(\\.jpg|\\.png)$", "fa");
		
		String name = "";
		int pixiv = 0, fa = 0, res = 0, res2 = 0;
		AbstractScrapper p1 = null;
		File temp = null, temp2 = null;
		for(File f : ((ProcessFile) fileProcessor).getList()) {
			name = f.getName();
			if(utils.getWebsite(name).equals("pixiv")) {
				System.out.println(name+"\t"+utils.getID(name));
				try {
					p1 = new PixivScrapper(utils.getID(name),
							"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0");
					res = p1.connect();
					if(res == 0) {
						p1.buildPage();
						System.out.println(p1.getAuthor());
						temp = new File("K:\\(｡･｀ω´･｡)\\2d\\kemono\\Sorted\\Pixiv\\"+p1.getAuthor()+"\\"+name);
						temp2 = new File("K:\\(｡･｀ω´･｡)\\2d\\kemono\\Sorted\\Pixiv\\"+p1.getAuthor());
						temp2.mkdirs();
						f.renameTo(temp);
					}
					else if(res == 404) {
						temp = new File("K:\\(｡･｀ω´･｡)\\2d\\kemono\\Sorted\\Pixiv\\_Deleted\\"+name);
						f.renameTo(temp);
					}
					else if(res == 403) {
						temp = new File("K:\\(｡･｀ω´･｡)\\2d\\kemono\\Sorted\\Pixiv\\_Private\\"+name);
						f.renameTo(temp);
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
				pixiv++;
			}
			/*else if(utils.getWebsite(name).equals("fa")) {
				System.out.println(name+"\t"+utils.getID(name));
				try {
					p1 = new FAScrapper("", "Chrome");
					p1.login("Jimbei", "Dratini147");
					((FAScrapper) p1).breakDownFileName(name);
					res = ((FAScrapper) p1).performSearch();
					res2 = p1.connectWithCookies();
					
					if(res == 0 && res2 == 0) {
						if(!name.equals("1374166767.trunorth_marmarmarmarmarmarmarmarmarmarmarmar.jpg")) {
							p1.buildPage();
							System.out.println(p1.getAuthor());
							temp = new File("K:\\(｡･｀ω´･｡)\\2d\\kemono\\Sorted\\FA\\"+p1.getAuthor()+"\\"+name);
							temp2 = new File("K:\\(｡･｀ω´･｡)\\2d\\kemono\\Sorted\\FA\\"+p1.getAuthor());
							temp2.mkdirs();
							f.renameTo(temp);
						}
					}
					else if(res2 == 404) {
						temp = new File("K:\\(｡･｀ω´･｡)\\2d\\kemono\\Sorted\\FA\\_Deleted\\"+name);
						f.renameTo(temp);
					}
					else if(res2 == 403) {
						temp = new File("K:\\(｡･｀ω´･｡)\\2d\\kemono\\Sorted\\FA\\_Private\\"+name);
						f.renameTo(temp);
					}
					else if(res == 400) {
						temp = new File("K:\\(｡･｀ω´･｡)\\2d\\kemono\\Sorted\\FA\\_NotFound\\"+name);
						f.renameTo(temp);
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
				fa++;
			}*/
		}
		System.out.println(((ProcessFile) fileProcessor).getList().size()+" images browsed!");
		System.out.println(pixiv+" pixiv images!");
		System.out.println(fa+" FA images!");
	}

}
